<?php


$m=new usql();
    class usql{
    	private $db;
    	public $sql=array("from"=>"","where"=>"","order"=>"","limit"=>"","update"=>"","select"=>"","set"=>"","delete"=>"","insert"=>"");
    	public $sentence;
    	function __construct($type="mysql",$host="localhost",$name="test",$user="root",$pass="maysrpldn"){
    		$dsn=$type.":host=".$host.";dbname=".$name;
    		$this->db=new PDO($dsn,$user,$pass);
    	}
    	function select($sel="*"){
    		$this->sql['select']=" SELECT ".$sel;
    		return $this;
    	}
    	function where($info){
    		$this->sql['where']=" WHERE ";
    		if(is_array($info)){
    			if(strtolower($info['_logic'])=="or"){
    				unset($info['_logic']);
    				foreach ($info as $key => $value) {
    					$this->sql['where']=$this->sql['where']." OR ".$key." = ".$value;
    				}

    			}elseif(strtolower($info['_logic'])=="and"){
    				unset($info['_logic']);
    				foreach ($info as $key => $value) {
    					$this->sql['where']=$this->sql['where']." AND ".$key." = ".$value;
    				}
    			}else{
    				if(count($info)==1){
    					$key=key($info);
    					$value=$info[$key];
    					$this->sql['where']=" WHERE ".$key."='".$value."'";
    				}else{
    					foreach ($info as $key => $value) {
    						$this->sql['where']=$this->sql['where']." AND ".$key." = ".$value;
    					}
    				}
    			}
    		}else{
    			$this->sql['where']=" WHERE ".$info;
    		}
    		return $this;

    	}
    	function order($key,$type="desc"){
    		$this->sql['order']=" ORDER BY ".$key." ".$type;
    		return $this;
    	}
    	function from($table){
    		$this->sql['from']=" FROM ".$table;
    		return $this;
    	}
    	function limit($start=1,$count=10){
    		$this->sql['limit']=" LIMIT ".$start." ".$count;
    		return $this;
    	}
    	function update($table){
    		$this->sql['upadte']=" UPDATE ".$table;
    		return $this;
    	}
    	function set($array){
    		$this->sql['set']=" SET ";
    		foreach ($array as $key => $value) {
    			$this->sql['set']=$this->sql['set']." ".$key."=".$value.",";
    		}
    		$this->sql['set']=substr($this->sql['set'], 0,-1);
    		return $this;
    	}
    	function delete($table){
    		$this->sql['delete']=" DELETE ".$table;
    		return $this;
    	}
    	function insert($table,$array){
    		$this->sql['insert']="INSERT INTO ".$table;
    		foreach ($array as $key => $value) {
    			$k_a[]=$key;
    			$v_a[]=$value;
    		}
    		$str_1=implode(",", $k_a);
    		$str_2=implode(",", $v_a);
    		$this->sentence=$this->sql['insert']." (".$str_1.") VALUES ( ".$str_2.")";
    	}
    	function query($sql){
    		return $this->db->query($sql);//SELECT
    	}
    	function exec($sel){
    		return $this->db->exec($sql);//INSERT UPADTE DELETE
    	}
    	function s_del($array){//简的通过一个key对应value删除
    		$key=key($array);
    		$value=$array[$key];
    		$this->sentense="DELETE".$this->sql['from']."WHERE ".$key."='".$value."'";
    		$this->exec($this->sentence);
    	}

    }